#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

import os

Import('BuildEnv')
env = BuildEnv.Clone()

sources = [
    'MANIFEST.in',
    'README.md',
    'setup.py',
    'requirements.txt',
    'test-requirements.txt',
    'opencontrail_vrouter_netns/__init__.py',
    'opencontrail_vrouter_netns/common.py',
    'opencontrail_vrouter_netns/vrouter_netns.py',
    'opencontrail_vrouter_netns/haproxy_process.py',
    'opencontrail_vrouter_netns/vrouter_docker.py',
    'opencontrail_vrouter_netns/daemon_start.py',
    'opencontrail_vrouter_netns/daemon_stop.py',
    'opencontrail_vrouter_netns/instance_provisioner.py',
    'opencontrail_vrouter_netns/lxc_manager.py',
    'opencontrail_vrouter_netns/network_manage.py',
    'opencontrail_vrouter_netns/vrouter_control.py',
    'opencontrail_vrouter_netns/lxc_docker.py',
    'opencontrail_vrouter_netns/linux/__init__.py',
    'opencontrail_vrouter_netns/linux/ip_lib.py',
    'opencontrail_vrouter_netns/linux/utils.py',
    'opencontrail_vrouter_netns/tests/__init__.py',
    'opencontrail_vrouter_netns/tests/test_vrouter_netns.py',
    'opencontrail_vrouter_netns/tests/test_vrouter_docker.py',
    'opencontrail_vrouter_netns/cert_mgr/__init__.py',
    'opencontrail_vrouter_netns/cert_mgr/barbican_cert_manager.py',
    'opencontrail_vrouter_netns/cert_mgr/cert_manager.py',
    'opencontrail_vrouter_netns/cert_mgr/kubernetes_cert.py',
    'opencontrail_vrouter_netns/cert_mgr/openstack_cert.py',
    'opencontrail_vrouter_netns/cert_mgr/tls.py',
]

cd_cmd = 'cd ' + Dir('.').path + ' && '
sdist_gen = env.Command('dist', sources, cd_cmd + 'python setup.py sdist')
env.Alias('controller/src/vnsw/opencontrail_vrouter_netns:sdist', sdist_gen)

env.Alias('install', env.InstallAs(env['INSTALL_CONF'] + '/contrail-lbaas-auth.conf',
            'opencontrail_vrouter_netns/contrail-lbaas-auth.conf'))
test_sources = sources
packages = [
    '#build/noarch/contrail-vrouter-api/dist/contrail-vrouter-api-1.0.tar.gz',
]

def BuildPyTestSetup(env, target, source):
    file = open(target[0].abspath, 'w')
    file.write("[easy_install]\nfind_links =")
    for pkg in source:
        dependency = env.File(pkg)
        file.write(" %s" % os.path.dirname(dependency.abspath))
    file.write("\n")
    file.close()
    return

def GeneratePyTestSetup(env, targets, source):
    """
    Generate a setup.cfg file that contains a list of directories
    where dependent packages can be found. The fact that package directory
    list is being given as a source automatically adds them as dependencies.
    """
    target = env.File('setup.cfg')
    return env.Command(target=target, source=source, action=BuildPyTestSetup);

env.Append(BUILDERS = {'PythonTestSetup': GeneratePyTestSetup})
test_sources += env.PythonTestSetup(source=packages)

test = env.Command('test.log', test_sources,
                   cd_cmd + 'python setup.py test > test.log')

env.Alias('controller/src/vnsw/opencontrail_vrouter_netns:test',
          env.AlwaysBuild(test))

# This add'l alias is to allow retry of UT in CI
env.Alias('controller/src/opencontrail-vrouter-netns/opencontrail_vrouter_netns:test', test)

if 'install' in BUILD_TARGETS:
    install_cmd = env.Command(None, sources,
                              cd_cmd + 'python setup.py install %s' %
                              env['PYTHON_INSTALL_OPT'])
    env.Alias('install', install_cmd)

# Local Variables:
# mode: python
# End:
